package com.baoxin.chenxixc4coach.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.News;
import com.baoxin.chenxixc4coach.java.FooterViewHolder;
import com.baoxin.chenxixc4coach.java.ToWebListener;
import com.bumptech.glide.Glide;

import java.util.List;


public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<News> newsList;
    private Context context;
    private ToWebListener listener;

    private int normalType = 0;
    private int footType = 1;
    private boolean hasMore;//是否有更多数据

    public NewsAdapter(Context context, ToWebListener listener) {
        this.context = context;
        this.listener = listener;
    }

    public void updateData(List<News> newsList, boolean hasMore) {
        this.newsList = newsList;
        this.hasMore = hasMore;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == normalType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news,
                    parent, false);
            final MyViewHolder holder = new MyViewHolder(view);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.openWeb(newsList.get(holder.getAdapterPosition()).getUrl());
                }
            });
            return holder;
        } else {
            View footerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer, parent, false);
            ProgressBar loadingPb = footerView.findViewById(R.id.loading_pb);
            TextView noMoreData = footerView.findViewById(R.id.no_more_data);
            if (hasMore) {
                loadingPb.setVisibility(View.VISIBLE);
                noMoreData.setVisibility(View.GONE);
            } else {
                loadingPb.setVisibility(View.GONE);
                noMoreData.setVisibility(View.VISIBLE);
            }
            FooterViewHolder footerViewHolder = new FooterViewHolder(footerView);
            return footerViewHolder;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            News news = newsList.get(position);
            ((MyViewHolder) holder).title.setText(news.getTitle());
            Glide.with(context)
                    .load(news.getImg())
                    .into(((MyViewHolder) holder).pic);
            ((MyViewHolder) holder).views.setText("" + news.getReadNumber());
            ((MyViewHolder) holder).comments.setText("" + news.getComments());
            ((MyViewHolder) holder).title2.setText(news.getType());
        } else {
            if (hasMore) {
                ((FooterViewHolder) holder).mPb.setVisibility(View.VISIBLE);
                ((FooterViewHolder) holder).mNoMoreData.setVisibility(View.GONE);
            } else {
                ((FooterViewHolder) holder).mPb.setVisibility(View.GONE);
                ((FooterViewHolder) holder).mNoMoreData.setVisibility(View.VISIBLE);
            }
        }

    }

    @Override
    public int getItemCount() {
        return newsList == null ? 0 : newsList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return footType;
        } else {
            return normalType;
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView title;
        ImageView pic;
        TextView views;
        TextView comments;
        TextView title2;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            title = itemView.findViewById(R.id.title);
            pic = itemView.findViewById(R.id.pic);
            views = itemView.findViewById(R.id.views);
            comments = itemView.findViewById(R.id.comments);
            title2 = itemView.findViewById(R.id.title2);
        }
    }
}
